﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseSimulation
{
    public class Transaction
    {
        private static int IdCounter = 0;
        public bool CanCommit { get; private set; }
        public bool Killed { get; private set; }
        public bool Completed { get; private set; }
        public List<DatabaseOperation> Operations { get; internal set; }
        public int ElapsedWaitOnObjetsToCommit { get; set; }
        public int ElapsedWaitOnTransactionsToCommit { get; set; }
        public int Id { get; private set; }

        public Transaction()
        {
            this.Id = Transaction.IdCounter++;
        }

        public void Kill()
        {
            this.Killed = true;
        }

        public void MarkComplete()
        {
            if (!this.CanCommit)
            {
                throw new Exception("Transaction cannot commit before being marked CanCommit");
            }
            this.Completed = true;
        }

        public void MarkCanCommit()
        {
            this.CanCommit = true;
        }

        internal static void ResetIdCounter()
        {
            Transaction.IdCounter = 0;
        }
    }
}